
<%--
  Created by IntelliJ IDEA.
  User: 113
  Date: 2020/12/5
  Time: 9:48
  To change this template use File | Settings | File Templates.
--%>

<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.servlet.jsp.tagext.FunctionInfo" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<jsp:useBean id="gwy" class="com.media.bean.gwy"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>

<%
    //加载驱动
    Class.forName("com.mysql.jdbc.Driver");

    //建立数据库连接
    String url="jdbc:mysql://localhost:3306/book";//数据库连接地址
    Connection connection= DriverManager.getConnection(url,"root","root");

    //关闭自动提交功能
    connection.setAutoCommit(false);


    String sql="insert into gwy (name,age,sex,入职日期,工资,职务,家庭住址,联系电话,id,) value (?,?,?,?,?,?,?,?,?)";//在PreparedStatement中使用问号代替实际参数
    PreparedStatement ps=connection.prepareStatement(sql);


    ps.setString(1,gwy.getName());
    ps.setInt(2,gwy.getAge());
    ps.setString(3,gwy.getSex());
    ps.setString(4,gwy.get入职日期());
    ps.setString(5,gwy.get工资());
    ps.setString(6,gwy.get职务());
    ps.setString(7,gwy.get家庭住址());
    ps.setString(8,gwy.set联系电话());
    ps.addBatch();


    ps.setString(1,gwy.getName()+"123");
    ps.setInt(2,gwy.getAge());
    ps.setString(3,gwy.getSex()+"123");
    ps.setString(4,gwy.get入职日期()+"123");
    ps.setString(5,gwy.get工资()+"123");
    ps.setString(6,gwy.get职务()+"123");
    ps.setString(7,gwy.get家庭住址()+"123");
    ps.setString(8,gwy.set联系电话()+"123");
    ps.setInt(9,20);
    ps.addBatch();

    int[] count={0};
    try{
        //事务
        count=ps.executeBatch();
        connection.commit();
    }catch (SQLException e){
        connection.rollback();
        e.printStackTrace();
    }


    if (count[0]>0){
        out.print("添加成功");
    }else{
        out.print("添加失败");
    }

    response.setHeader("refresh","3;url=home.jsp");
%>